package com.example.function;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public abstract class AviatorFunction extends AbstractFunction implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public String getName() {
        return getClass().getSimpleName();
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
            AviatorEvaluator.addFunction(this);
        }
    }
}